import {
  list
} from '../../../../api/ad'
import {
  getInfo
} from '../../../../api/mch'
import util from '../../../../utils/util'

let app = getApp();
Component({
  properties: {
    info: {
      type: Object
    }
  },
  pageLifetimes: {
    ready() {

      this.triggerEvent('getCanHeight')
    },
  },
  lifetimes: {
    attached() {
      var _this = this
      if (this.data.info.tagids != null) {
        this.data.tagIds = this.data.info.tagids.split(',')
        for (var i = 0; i < this.data.tagIds.length; i++) {
          this.data.tagIds[i] = parseInt(this.data.tagIds[i])
        }
      }
      this.loadAd()
      this.loadHead()
      this.setNavSize()

    }
  },



  /**
   * 组件的初始数据
   */
  data: {
    isChoose: 0,
    swipersIndex: 0,
    tagIds: [],
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
    photoList: [],
    titles: [],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    circular: true,
    interval: 3000,
    duration: 500,
    previousMargin: 0,
    color: [],
    c: '',
    nextMargin: 0,
    pageNum: 0,
    pageSize: 10,
    logo:'',
    startSwipers: false,
    transparent: 1,
    headerColor: 'ragb(255,255,255,0)',
    mch: {
      logo: '',
      name: ''
    },
    title: '',
    indexs: [],
    images: [],
  },

  /**
   * 组件的方法列表
   */
  created() {

  },
  methods: {
    bindChange(e) {
      let {
        current,
        source
      } = e.detail
      if (source === 'autoplay' || source === 'touch') {
        this.setData({
          title:this.data.titles[current],
          swipersIndex: current
        })
      }
     
    },
    toDetail(data) {
      if (data.currentTarget.dataset.type == 0) {
        wx.navigateTo({
          url: '/pages/shop/productDetail/productDetail?productId=' + data.currentTarget.dataset.id
        })
      } else if (data.currentTarget.dataset.type == 2) {
        wx.navigateTo({
          url: '/pages/hotspot/hotspot/index?id=' + data.currentTarget.dataset.id,
        })
      } else if (data.currentTarget.dataset.type == 3) {
        wx.navigateTo({
          url: '/pages/discovery/details/details?id=' + data.currentTarget.dataset.id
        })
      } else if (data.currentTarget.dataset.type == 4) {
        wx.navigateTo({
          url: '/pages/travel/travel/index?scenicSpotId=' + data.currentTarget.dataset.id
        })
      } else if (data.currentTarget.dataset.type == 5) {
        wx.navigateTo({
          url: '/pages/shop/shopDetail/index?shopId=' + data.currentTarget.dataset.id + '&mchId=' + app.globalData.mchId
        })
      } else if (data.currentTarget.dataset.type == 6) {
        wx.navigateTo({
          url: '/pages/cook/kitchenBrigade/kitchenBrigade?id=' + data.currentTarget.dataset.id + '&mchId=' + app.globalData.mchId
        })
      } else if (data.currentTarget.dataset.type == 7) {
        wx.navigateTo({
          url: '/pages/shop/shopDetail/index?shopId=' + data.currentTarget.dataset.id + '&mchId=' + app.globalData.mchId
        })
      } else if (data.currentTarget.dataset.type == 8) {
        wx.navigateTo({
          url: '/pages/coupon/coupon',
        })
      }else if (data.currentTarget.dataset.type == 9) {
        if(data.currentTarget.dataset.id == 367){
          wx.navigateTo({
            // url: `/pages/shop/index?name=${title}&tagId=${typeName}`
            url: `/pages/shop/index?name=${data.currentTarget.dataset.typeName}&tagId=${1398}`
          })
          return
        }
        wx.navigateTo({
          url: '/pages/index/wxPage/wxPage?id='+data.currentTarget.dataset.id 
        })
      } else if (data.currentTarget.dataset.type == 12) {
        wx.navigateTo({
          url: '/pages/member/webview/index?url='+data.currentTarget.dataset.adlist.typeName
        })
      }
    },
    searchClick() {
      wx.navigateTo({
        url: '/pages/goods/search/index'
      })
    },
    // changeBackground(e) {
    //   this.setData({
    //     c: this.data.color[e.detail.current]
    //   })
    // },
    detailsClick() {
      wx.navigateTo({
        url: '/pages/goods/product/details',
      })
    },
    loadHead(){
      getInfo(app.globalData.mchId).then(res=>{
        res.data.logo=util.thumbnail(res.data.logo,125,125)
          this.data.logo=res.data.logo

          this.setData({
            logo:this.data.logo
          })
      })
    },
    setNavSize: function () {
      var that = this,
        sysinfo = wx.getSystemInfoSync(),
        statusHeight = sysinfo.statusBarHeight,
        isiOS = sysinfo.system.indexOf('iOS') > -1,
        navHeight;
      console.log(sysinfo)
      if (!isiOS) {
        navHeight = 48;
      } else {
        navHeight = 44;
      }
      that.setData({
        status: statusHeight,
        navHeight: navHeight
      })
    },

    loadAd() {
      var _this = this
      list({
        mchId: app.globalData.mchId,
        tagIds: this.data.tagIds,
      }).then(res => {
        res.data.data.forEach(element => {
          element.thumbnail = util.thumbnail(element.thumbnail, 710, 200)
          
          this.data.titles.push(element.title)
        });
        this.data.title=this.data.titles[0]
        _this.data.images = res.data.data
        for (var i = 0; i < this.data.images.length; i++) {
          this.data.indexs.push(i)
        }
        this.setData({
          title:this.data.title,
          titles: this.data.titles,
          indexs: this.data.indexs,
          images: this.data.images
        })
      });
    },
    aaa() {
      console.log(this.data.info)
    }
  }
})